package cn.xz.xy.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ZhuanHuan {
	// 2转16进制
    public static String binaryString2hexString(String bString)  
    {  
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)  
            return null;  
        StringBuffer tmp = new StringBuffer();  
        int iTmp = 0;  
        for (int i = 0; i < bString.length(); i += 4)  
        {  
            iTmp = 0;  
            for (int j = 0; j < 4; j++)  
            {  
            
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);  
            }  
            tmp.append(Integer.toHexString(iTmp));  
        }  
        return tmp.toString();  
    }  
  //16转2进制
    public static String hexString2binaryString(String hexString)  
    {  
        if (hexString == null || hexString.length() % 2 != 0)  
            return null;  
        String bString = "", tmp;  
        for (int i = 0; i < hexString.length(); i++)  
        {  
            tmp = "0000"  
                    + Integer.toBinaryString(Integer.parseInt(hexString  
                            .substring(i, i + 1), 16));  
            bString += tmp.substring(tmp.length() - 4);  
        }  
        return bString;  
    }  
    //16字符串转byte
    public static byte[] hexStringToBytes(String hexString) {   
        if (hexString == null || hexString.equals("")) {   
            return null;   
        }
        hexString = hexString.replace(" ","").toUpperCase();
        int length = hexString.length() / 2;   
        char[] hexChars = hexString.toCharArray();   
        byte[] d = new byte[length];   
        for (int i = 0; i < length; i++) {   
            int pos = i * 2;   
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
        }   
        return d;   
    }   
    private static byte charToByte(char c) {   
        return (byte) "0123456789ABCDEF".indexOf(c);   
    }  
    //byte转16进制
    public static String bytesToHexString(byte[] src){   
        StringBuilder stringBuilder = new StringBuilder("");   
        if (src == null || src.length <= 0) {   
            return null;   
        }   
        for (int i = 0; i < src.length; i++) {   
            int v = src[i] & 0xFF;   
            String hv = Integer.toHexString(v);   
            if (hv.length() < 2) {   
                stringBuilder.append(0);   
            }   
            stringBuilder.append(hv);   
        }   
        return stringBuilder.toString();   
    }   
    public static String trans(int num, int base, int offset) { 
    	  if(num == 0) return "0"; //如果num=0,程序就不能工作了，所以加上这句代码，总比输出什么都没有要好
    	  char[] chs = {'0','1','2','3', //这里用到了查表法，以输出各种进制的特有表示符号
    	    '4','5','6','7',
    	    '8','9','A','B',
    	    'C','D','E','F'};
    	  char[] arrChar = new char[32]; //java中int型整数最多只有32位，所以定义一个32位数组足够装各种进制中的一位了
    	  int pos = arrChar.length; //定义一个索引指向char数组的长度加1的位置
    	  StringBuffer sb = new StringBuffer(); //存储各进制中的一位
    	  
    	  //计算出各进制中的一位并从char数组中的最后一位开始存储，方便打印
    	  while(num != 0) {
    	   int temp = num & base;
    	   arrChar[--pos] = chs[temp];
    	   num = num >>> offset;
    	  }
    	  
    	  //把char数组的元素添加到StringBuffer中，以返回一个字符串
    	  for(int i = pos; i < arrChar.length; i++)
    	   sb.append(arrChar[i]);
    	  return sb.toString(); //返回一个字符串
    	 }
    //提取不同的元素
    public static List<Integer> list(int[] a){
		Set<Integer> set=new HashSet<Integer>();
		List<Integer> list=new ArrayList<Integer>();
		for (int i : a) {
			set.add(i);
		}
		Iterator iterator=set.iterator();
		while(iterator.hasNext()){
			list.add((Integer)iterator.next());
		}
		return list;
	}
}
